$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.form = new xhForm($('#xh_form'));
            this.form.parse();
            $('[name=iProdId],[name=iCIId]').select2();
            this.form.bindRequired($('#bt_modal_confirm', parent.document));
            this.listener();
        },
        listener:function () {
            var $this = this;
            //double处理
            $('.doubleInput').on('change',function () {
                $this.funObj().toFormatDouble($(this).val(), $(this));
            });
            //点击保存
            $('#bt_modal_confirm', parent.document).click(function () {
                $this.funObj().submitFormAction();
            });
            //商品点中
            $('[name=iProdId]').on('change', function () {
                $this.funObj().triggerChange($(this).val());
            });

            $('#bt_list_del').click(function () {
                $this.funObj().delInAction();
            })

        },
        funObj:function () {
            var $this = this;
            return {
                toFormatDouble:function (number,dom) {
                    var f = parseFloat(number);
                    if (isNaN(f)) {
                        dom.val('0.00');
                    }
                    var f = Math.round(number*100)/100;
                    var s = f.toString();
                    var rs = s.indexOf('.');
                    if (rs < 0) {
                        rs = s.length;
                        s += '.';
                    }
                    while (s.length <= rs + 2) {
                        s += '0';
                    }
                    dom.val(s);
                },
                submitFormAction:function () {
                    $this.form.submit('ims/detail/saveIn', {}, function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                        window.top.xhTop.closeModal();
                    })
                },
                triggerChange:function (val) {
                    if (!val) {
                        return false;
                    }
                    xhExtra.ajaxRequest('ims/produce/getProdInfo', {id:val},{},function (res) {
                        if (res.sMeasure) {
                            $('[name=sMeasure]').val(res.sMeasure);
                        }
                        if (res.dInPrice) {
                            $('[name=dInPrice]').val(res.dInPrice);
                        }
                    })
                },
                delInAction:function () {
                    var ids = $this.dataGrid.getSelectValues('iPInId');
                    xhExtra.askChangeStatus('ims/detail/delIn', {ids:ids}, $this.dataGrid, '确定要删除吗？', function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.closeModal();
                    })
                }
            }
        }
    }
};